#include <string>
#include <iostream>
#include <cmath>


#include <chrono>
#include <thread>

#include <fstream>
#include <algorithm>

#include "theme.h"

Theme::Theme()
{
    readTheme();
}

Theme::~Theme()
{
}

int Theme::getGameBoardBG() const
{
	return this->GameBoardBG;
}

int Theme::getThemeindex() const
{
    return this->Themeindex;
}

SDL_Color Theme::getCountdownRect() const
{
	return this->CountdownRect;
}

SDL_Color Theme::getCountdownText() const
{
	return this->CountdownText;
}

SDL_Color Theme::getInstructionColor() const
{
    return this->InstructionColor;
}

SDL_Color Theme::getGameBoardColor() const
{
    return this->GameBoardColor;
}

SDL_Color Theme::getRestartColor() const
{
    return this->RestartColor;
}

SDL_Color Theme::getRestartOriColor() const
{
    return this->RestartOriColor;
}

void Theme::setTheme(int index)
{
	switch (index) {
	case 1:
		this->GameBoardBG = index;
		this->Themeindex = index;
		this->CountdownRect = { 231, 235, 54 };
		this->CountdownText = { 0, 0, 0 };
        this->InstructionColor = { 10, 10, 233 };
        this->GameBoardPoints = { 0, 0, 0, 255 };
        this->RestartColor = { 122, 115, 221, 150 };
        this->RestartOriColor = { 80, 74, 167, 150 };
        this->GameBoardColor = { 40, 0, 100 , 180 };
        this->font = TTF_OpenFont("./source/fonts/bitlight10.ttf", 16);
        break;
    case 2:
        this->GameBoardBG = index;
        this->Themeindex = index;
        this->CountdownRect = { 165, 105, 189 };
        this->CountdownText = { 0, 0, 0 };
        this->InstructionColor = { 209, 58, 165 };
        this->GameBoardPoints = { 0, 0, 0, 255 };
        this->RestartColor = { 245, 151, 212, 150 };
        this->RestartOriColor = { 191, 99, 163, 150 };
        this->GameBoardColor = { 168, 69, 124 , 180 };
        this->font = TTF_OpenFont("./source/fonts/bitlight10.ttf", 16);
    };
    this->writeTheme();
}

//Read the theme index
bool Theme::readTheme()
{
    std::fstream fhand(this->RecordThemeFilePath, fhand.binary | fhand.in);
    if (!fhand.is_open())
    {
        return false;
    }

    int temp;

    if (fhand.read(reinterpret_cast<char*>(&temp), sizeof(temp))) {
        this->setTheme(temp);
    }
    else {
        return false;
    }

    fhand.close();
    return true;
}

//Write the theme index
bool Theme::writeTheme()
{
    // trunc: clear the data file
    std::fstream fhand(this->RecordThemeFilePath, fhand.binary | fhand.trunc | fhand.out);
    if (!fhand.is_open())
    {
        return false;
    }
    fhand.write(reinterpret_cast<char*>(&this->Themeindex), sizeof(this->Themeindex));

    fhand.close();
    return true;
}
